package com.cy.module;

import com.alibaba.fastjson.JSONObject;
import com.cy.config.EnumConst;
import com.cy.ext.plugin.route.ControllerBind;
import com.cy.model.License;
import com.cy.model.User;
import com.cy.shiro.QyUsernamePasswordToken;
import com.jfinal.plugin.activerecord.Page;
import org.apache.commons.lang3.StringUtils;
import org.apache.shiro.SecurityUtils;
import org.apache.shiro.subject.Subject;

import java.util.Map;

@ControllerBind(controllerKey = "/clmp/license")
public class LicenseController extends WebApiController {

    @Override
    public void index() {
        License license = getModel(License.class);
        int page = getParaToInt("pageIndex");
        int pageSize = getParaToInt("limit");
        Map<String, String[]> parasMap = getParaMap();
        String other = "";
        String selectAll ="`license_id`,license.`user_id`,`license_available`,`license_consumption`,`license_frozen`,`last_os`,license.`status`";
        if (null != parasMap && parasMap.size() > 0){
            String phone = "";
            if (null != parasMap.get("phone")){
                phone = parasMap.get("phone")[0];
            }
            String userName ="";
            if (null != parasMap.get("userName")){
                userName = parasMap.get("userName")[0];
            }
            if(StringUtils.isNotBlank(phone)){
                license.put("user.mobile",phone);
            }
            if(StringUtils.isNotBlank(userName)){
                license.put("nick_name",userName);
            }
            other = " , user user WHERE user.user_id = license.user_id ";
            selectAll+=",user.`mobile` mobile ,user.`nick_name` nick_name  ";
        }


        Page<License> licensePage = License.dao.paginate(page, pageSize, selectAll, license,other);
        renderBuiPage(licensePage, 200);
    }

    @Override
    public void update() {
        int id = getParaToInt();
        License tempLicense = getModel(License.class);
        License license = License.dao.findById(id);
        license.cover(tempLicense);

        if (license.update()) {
            renderSuccess();
        } else {
            renderFail();
        }
    }

    @Override
    public void save() {
        License license = getModel(License.class);
        if (license.save()) {
            renderSuccess();
        } else {
            renderFail();
        }
    }

    @Override
    public void delete() {
        String[] ids = getPara().split("-");
        for (String id : ids) {
            License.dao.deleteById(id);
        }
        renderSuccess();
    }

    public void getLicenseAccount(){
       JSONObject json = new JSONObject();
       User user = getLoginUser();
       if (null == user){
           String account = getPara("account");
           String password = getPara("password");
           if (StringUtils.isEmpty(account) || StringUtils.isEmpty(password)) {
               renderStatus(EnumConst.RetCode.NOT_SIGNIN.getCode());
               return;
           }
           Subject currentUser = SecurityUtils.getSubject();
           QyUsernamePasswordToken token = new QyUsernamePasswordToken(account, password,QyUsernamePasswordToken.Type.USER.getValue());
           try {
               currentUser.login(token);
               initLogger();
               getLog().info("login To :" + account + ",and success!");

           }catch (Exception e){
               renderStatus(EnumConst.RetCode.NOT_SIGNIN.getCode());
               return;
           }

           user = getLoginUser();
       }
       License license = License.dao.findByUserId(user.getUserId());
       json.put("code",EnumConst.RetCode.SUCCESS.getCode());
       json.put("license",license);
       json.put("user",user.getResultUser());
       renderJson(json);
    }
}
